//
//  myMovieViewController.m
//  UI-豆瓣项目APP
//
//  Created by liuxin on 15/11/4.
//  Copyright © 2015年 孙鹏. All rights reserved.
//

#import "myMovieViewController.h"
#import "CollectMovieDB.h"
#import "MovieList.h"
#import "MovieDetailViewController.h"
#import "CollectMovieDB.h"
@interface myMovieViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@end

@implementation myMovieViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"LoginName"];
    self.allSharedMovie = [[CollectMovieDB shareDataBase]selectMoview:name];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
//    self.tableView.backgroundColor = [UIColor orangeColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"myMovie"];
    [self.view addSubview:self.tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allSharedMovie.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identify = @"myMovie";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    MovieList *mov = [self.allSharedMovie objectAtIndex:indexPath.row];
    cell.textLabel.text =mov.movieName;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieList *mov = [self.allSharedMovie objectAtIndex:indexPath.row];
    MovieDetailViewController *mo = [[MovieDetailViewController alloc]init];
    mo.movieID = mov.movieId;
    [self.navigationController pushViewController:mo animated:YES];
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    //    设置tableView编辑状态,随着按钮状态改变而改变
    [self.tableView setEditing:editing animated:animated];
}
#pragma mark -- 每行的编辑样式(删除/添加)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        MovieList *Mo = [self.allSharedMovie objectAtIndex:indexPath.row];
        NSString *movieName = Mo.movieName;
        NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"LoginName"];
//        [[CollectMovieDB shareDataBase]deleteMovie:movieName];
        [[CollectMovieDB shareDataBase]deleteMovie:movieName UserName:userName];
        [self.allSharedMovie removeObjectAtIndex:indexPath.row];
    }
     [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end